<?
define('SSLIB_Sorter', TRUE);
if(!defined("SSLIB_Template")) die("Template class missing");
if(!defined("SSLIB_Request")) die("Request class missing");
class Sorter 
{
	private $fields, $desc, $s;
	function Sorter($fields)
	{
		$this->fields = $fields;
		$this->desc = Request::Int('desc');
		$this->s = Request::Int('s');
		$keys = array_keys($fields);
		if($this->s == null) $this->s = 1;
		if($this->desc == null) $this->desc = 1;
	}
	
	/*function Url($url)
	{
		
		$ret = "";
		if($this->s)	$ret .= "s=$this->s";
		if($this->desc)	$ret .= "&desc=1";
		if($ret)
			return $url.((stripos($url, '?') === FALSE)?'?':'&').$ret;
		else 
			return $url;
	}*/
	function Order($desc = false)
	{
		$this->desc = $desc;
		$f = array_values($this->fields);
		$ret = ' ORDER BY ';
		if(sizeof($this->fields)-1 < ($this->s -1))
			$this->s = 1;
		$ret .= $f[$this->s-1];
		$ret .= $this->desc?' DESC ':' ASC ';
		
		$this->updateRequired();
		
		return $ret;
	}
	private function updateRequired()
	{
		if($this->s)	Request::RequiredSet('s',$this->s);
		if($this->desc)	Request::RequiredSet('desc',1);
	}
	function Navigation($template)
	{
		$i = 0;
		$f = "";
		foreach($this->fields as $key => $val)
		{
			$i++;
			
			$f .= Template::CompileFile
				(
					array('value' => $i, 'name' => $key),
					$template.(($this->s == $i)?'active':'normal').'.htm'
				);
		}
		
		$this->updateRequired();
		
		return Template::CompileFile(array('framecontent'=>$f), $template.'frame.htm');
	}
	
}
?>